package com.base.vistter.iframe.service;

import com.base.vistter.iframe.entity.InverterDevicesPowerSummary;
import com.base.vistter.iframe.mapper.InverterDevicesPowerSummaryMapper;
import com.base.vistter.iframe.model.DevicesSummaryPowerRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class InverterDevicesPowerSummaryService extends BaseService<InverterDevicesPowerSummaryMapper, InverterDevicesPowerSummary> {

    @Transactional(readOnly = true)
    public List<InverterDevicesPowerSummary> statisticsDevices(DevicesSummaryPowerRequest devicesSummaryPowerRequest) {
        String column = Arrays.stream(devicesSummaryPowerRequest.getColumns()).map(item -> "SUM(`" + item + "`) AS " + item).collect(Collectors.joining(","));
        List<InverterDevicesPowerSummary> list = this.baseMapper.statisticsDevicesSummary(column, devicesSummaryPowerRequest.getDevicesId());
        //如果是本年统计，则添加今日统计数据
//        if (DateUtils.isCurrentYear(devicesYearPowerRequest.getTime())) {
//            list.add(this.baseMapper.getTodayPower(column, devicesYearPowerRequest.getDevicesId(), DateUtils.getTodayStartTime(), DateUtils.getTodayEndTime()));
//        }
        return list;
    }

    @Transactional(readOnly = true)
    public List<InverterDevicesPowerSummary> statisticsSummary(Integer stationId) {
        return this.baseMapper.statisticsSummary(stationId);
    }

}
